/*

Summary:
Used to remove specified chars from string's head and tail.

Remark:
Default chars to remove are blank chars: ' ', \t, \r and \n.

For non-const string, modification would apply to it, and
Trim returns nothing. For const string, Trim would return
a new string.

Version:
0.1.0

LastUpdatedDate:
2012-08-09

*/


#pragma once
#include <string>


namespace Zaf {
namespace String {

#define __Zaf_Trim_DefaultBlankCharsA__ (" \t\r\n")
#define __Zaf_Trim_DefaultBlankCharsW__ (L" \t\r\n")


//General purpose function.
template<typename CharType>
inline void Trim(std::basic_string<CharType>& str, const CharType* const blankChars) {
	str.erase(str.find_last_not_of(blankChars) + 1);
	str.erase(0, str.find_first_not_of(blankChars));
}

//Forward declaration.
template<typename CharType>
inline void Trim(std::basic_string<CharType>& str);

//Implementation of narrow char.
template<>
inline void Trim<char>(std::string& str) {
	Trim(str, __Zaf_Trim_DefaultBlankCharsA__);
}

//Implementation of wide char.
template<>
inline void Trim<wchar_t>(std::wstring& str) {
	Trim(str, __Zaf_Trim_DefaultBlankCharsW__);
}



//General purpose function.
template<typename CharType>
inline std::basic_string<CharType> Trim(const std::basic_string<CharType>& str, const CharType* const blankChars) {
	int beginIndex = str.find_first_not_of(blankChars);
	if (beginIndex == -1) {
		return std::basic_string<CharType>();
	}
	int endIndex = str.find_last_not_of(blankChars) + 1;
	return str.substr(beginIndex, endIndex - beginIndex);
}

//Forward declaration.
template<typename CharType>
inline std::basic_string<CharType> Trim(const std::basic_string<CharType>& str);

//Implementation of narrow char.
template<>
inline std::string Trim<char>(const std::string& str) {
	return Trim(str, __Zaf_Trim_DefaultBlankCharsA__);
}

//Implementation of wide char.
template<>
inline std::wstring Trim<wchar_t>(const std::wstring& str) {
	return Trim(str, __Zaf_Trim_DefaultBlankCharsW__);
}


}
}